<?php
namespace Admin\Controller;
use Think\Controller;
class PhotoController extends BaseController {
    public function index(){
    	$where = array();
        if (I('key')) {
            $where['title'] = array('like','%'.I('key').'%');
        }
    	$this->list = M("Photo")->Where($where)->Where($map)->order('addtime desc')->Select();
		$this->display();
    }
    public function release(){
		if (!IS_POST) {
        	$this->display();
        }
        if (IS_POST) {
         	$model = M('Photo');
         	$photo = I('post.thumb');
         	$img = reset($photo);
         	$thumb = [];
				foreach($photo['img'] as $k=>$v){
				$thumb[] = [
					'img'=>$v,
					'alt' =>$photo['alt'][$k],
				];
			}
         	$data = array(
         		'title' => I('post.title'),
         		'keywords' => I('post.keywords'),
         		'description' => I('post.description'),
         		'sort' => I('post.sort'),
         		'type' => I('post.type'),
         		'author' => I('post.author'),
         		'uid' => I('post.uid'),
         		'img' => reset($img),
				'thumb' => serialize($thumb),
				'addtime' => time(),
			);
			if(!$model->create($data)){
                exit($model->getError());
         	}else{         		
              	if($model->add()) {
                    $this->success("添加成功", U('photo/index'),1);
               	}else{
                    $this->error("添加失败", U('photo/index'),1);
                }
         	}
        }
    }
    public function editor($id){
		if (!IS_POST) {
        	$map['id'] = $id;       	
			$info = M("Photo")->where($map)->find();
			$thumb = unserialize($info['thumb']);
			$this->assign('info',$info);
			$this->assign('photo',$thumb);
 	    	$this->display();
        }
        if (IS_POST) {
         	$model = M('Photo');
         	$data = array(
         		'id' => I('post.id'),
         		'title' => I('post.title'),
         		'keywords' => I('post.keywords'),
         		'description' => I('post.description'),
         		'sort' => I('post.sort'),
         		'type' => I('post.type'),
         		'author' => I('post.author'),
         		'uid' => I('post.uid'),
				'addtime' => time(),
			);
         	if($model->create($data)){
         		if ($model->save()) {
                    $this->success("修改成功", U('photo/index'),1);
                } else {
                    $this->error("修改失败");
                }
         	}else{
         		$this->error($model->getError());
                exit();
         	}
        }
    }
    public function delete($id){
		if (!empty($id)) {
            $result = M("Photo")->delete($id);
            if (false !== $result) {
                $this->success('删除成功！');
            } else {
                $this->error('删除出错！');
            }
        } else {
        	
            $this->error('编号错误！');
        }
    }
    public function state($id){
        //查询该条信息的state字段值
        $result = M("Photo")->find($id);
        //更新字段 
        if($result['state'] == 1){
          $data['state']=0;
        }
        if($result['state'] == 0){
          $data['state']=1;
        }
       	  $data['id']=$id;
        if(M("Photo")->save($data)){       	
            $this->success('状态更新成功!',U('photo/index'),1);
        }else{
            $this->error('状态更新失败',U('photo/index'),1);
        }
    }

}



